package com.situ.crm.service.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Customer;
import com.situ.crm.mapper.CustomerMapper;
import com.situ.crm.service.ICustomerService;

@Service
public class CustomerServiceImpl implements ICustomerService {
	@Autowired
	private CustomerMapper customerMapper;
	
	@Override
	public ServerResponse pageList(Integer page, Integer limit, Customer customer) {
		PageHelper.startPage(page,limit);
		List<Customer> list = customerMapper.pageList(customer);
		
		PageInfo pageInfo = new PageInfo<>(list);
		Integer count = (int)pageInfo.getTotal();
		return ServerResponse.createSuccess("查找成功", count, list);
	}
	
	//添加
	@Override
	public ServerResponse insert(Customer customer) {
		try {
			Integer count = customerMapper.insert(customer);
			if (count == 1) {
				return ServerResponse.createSuccess("添加成功");
			} else {
				return ServerResponse.createError("添加失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}

	//根据id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = customerMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//批量删除
	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArrays = ids.split(",");
		try {
			Integer count = customerMapper.deleteAll(idArrays);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//修改查询
	@Override
	public Customer select(Integer id) {
		return customerMapper.selectByPrimaryKey(id);
	}

	//修改
	@Override
	public ServerResponse update(Customer customer) {
		try {
			Integer count = customerMapper.updateByPrimaryKeySelective(customer);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}

	//导出报表
	@Override
	public void exportExcel(HttpServletResponse response) {
		// 1.查询用户列表
		List<Customer> list = customerMapper.pageList(new Customer());
		for (Customer customer : list) {
			System.out.println(customer);
		}
		// 2.写到excel
		// 1、创建工作簿：Workbook。
		XSSFWorkbook workbook = new XSSFWorkbook();
		// 2、创建工作表：Sheet。
		XSSFSheet sheet = workbook.createSheet("客户列表");
		// 设置默认的列宽
		sheet.setDefaultColumnWidth(25);
		// 2.1创建合并单元格对象
		CellRangeAddress cellRangeAddress = new CellRangeAddress(2, 6, 1, 5);
		sheet.addMergedRegion(cellRangeAddress);
		// 3、创建行：Row。
		XSSFRow rowTitle = sheet.createRow(2);
		// 4、创建单元格：Cell。
		XSSFCell cellTitle = rowTitle.createCell(1);
	
		XSSFCellStyle style = createStyle(workbook, 25);
		cellTitle.setCellStyle(style);
		cellTitle.setCellValue("客户列表");
	
		XSSFRow rowHead = sheet.createRow(7);
		/* 创建单元格 */
		String[] array = { "编号", "姓名", "性别", "办公电话", "固定电话"};
		for (int i = 0; i < array.length; i++) {
			XSSFCell cellHead = rowHead.createCell(i + 1);
			XSSFCellStyle headStyle = createStyle(workbook, 13);
			cellHead.setCellStyle(headStyle);
			cellHead.setCellValue(array[i]);
		}
	
		XSSFCellStyle styleCenter = workbook.createCellStyle();
		styleCenter.setAlignment(XSSFCellStyle.ALIGN_CENTER);// center
																// horizontal
																// alignment
		styleCenter.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中
	
		// 遍历输出数据库中的数据到excel中
		for (int i = 0; i < list.size(); i++) {
	
			XSSFRow row = sheet.createRow(8 + i);
			XSSFCell cellId = row.createCell(1);
			cellId.setCellStyle(styleCenter);
			cellId.setCellValue(list.get(i).getId());
			XSSFCell cellName = row.createCell(2);
			cellName.setCellStyle(styleCenter);
			cellName.setCellValue(list.get(i).getName());
			XSSFCell cellGender = row.createCell(3);
			cellGender.setCellStyle(styleCenter);
			cellGender.setCellValue(list.get(i).getGender());
			XSSFCell cellOfficePhone = row.createCell(4);
			cellOfficePhone.setCellStyle(styleCenter);
			cellOfficePhone.setCellValue(list.get(i).getOfficePhone());
			XSSFCell cellPhone = row.createCell(5);
			cellPhone.setCellStyle(styleCenter);
			cellPhone.setCellValue(list.get(i).getPhone());
		}
	
		response.setContentType("applicaiton/x-excel");
		// 3.以附件的形式返回给浏览器
		ServletOutputStream outputStream = null;
		try {
			// response.setHeader("Content-Disposition", "attachment;filename="
			// + "aaa.xls");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + new String("客户列表.xlsx".getBytes(), "ISO-8859-1"));
			outputStream = response.getOutputStream();
			workbook.write(outputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 输出excel到文件
			if (workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
		private XSSFCellStyle createStyle(XSSFWorkbook workbook, int fontSize) {
			// 创建单元格样式
			XSSFCellStyle style = workbook.createCellStyle();
			style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// center horizontal
															// alignment
			style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中
			// 创建字体
			XSSFFont font = workbook.createFont();
			font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
			font.setFontHeightInPoints((short) fontSize);// 设置字体大小
			// 加载字体到样式
				style.setFont(font);
				return style;
		}

		//查询所有的信息
		@Override
		public List<Customer> selectAll() {
			return customerMapper.selectByAll();
		}
}


